﻿using ExpressesClass.Model;
using System;
using System.Drawing;
using System.Drawing.Printing;
using ZXing;
using ZXing.Common;

namespace ExpressesClass.IPrints
{
    public class DepponPrint : IPrint
    {
        private Kddh kddh;
        private int printCur;
        private DateTime now;

        private int _w, _5, _8, _10, _12, _13, _16, _20, _25, _35, _45, _50;

        public DepponPrint(Kddh kddh, int printCur, DateTime now)
        {
            this.kddh = kddh;
            this.printCur = printCur;
            this.now = now;
            _w = 290;
            _5 = 20;
            _8 = 36;
            _10 = 40;
            _12 = 48;
            _13 = 52;
            _16 = 64;
            _20 = 80;
            _25 = 100;
            _35 = 140;
            _45 = 180;
            _50 = 200;
        }

        public void Print(string printer)
        {
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printer;
            printDoc.DefaultPageSettings.PaperSize = new PaperSize("DepponSize", 290, 500);
            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 
            printDoc.Print();
        }

        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            //设置高质量插值法   
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度   
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //消除锯齿
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //打印时间
            PrintPrintTime(g, 5, _8, _35, _8);
            //快递类型
            PrintShipName(g, _50, 10, _35, _12);
            //大笔
            PrintSortingCode(g, 10, _12 + 13, _w, _20);
            //收货人行政区
            PrintCity(g, 10, _12 + _16 + 5, _w, _8);
            //当前打印号
            PrintCurNum(g, _50 + _13, _12 + _16 + 5, _35, _8);
            //收
            PrintReceive(g, 5, _12 + _16 + _8 + _5 + 5, 28, 28);
            //收件人
            PrintReceiveMan(g, _8 + 5, _12 + _16 + _8 + 2, _w - _8, _5);
            //收件人电话
            PrintReceivePhone(g, _8 + 5, _12 + _16 + _8 + _5 + 2, _w - _8, _5);
            //收件人地址
            PrintReceiveAddress(g, _8 + 5, _12 + _16 + _8 + _5 + _5 + 2, _w - _8, _10);
            //寄
            PrintSend(g, 7, _12 + _16 + _8 + _20 + 4, 26, 26);
            //寄件人
            PrintSendMan(g, _8 + 5, _12 + _16 + _8 + _20 + 5, _w, _8);
            //实名认证
            PrintRealName(g, _50 + _5, _12 + _16 + _8 + _20 + 5, _w, _8);
            //横向面单号
            PrintHorMailNo(g, 5, _12 + _16 + _8 + _20 + _8 + 5, _w - _5, _13);
            //运单号
            PrintMailNo(g, 8, _12 + _16 + _8 + _20 + _8 + _13 + 5, _w - _5, _13);
            //已检视
            PrintLook(g, _50 + _5, _12 + _16 + _8 + _20 + _8 + _13 + _5 + 5, _w - _5, _10);
            //保价金额
            PrintInsuranceValue(g, _45 - 10, _12 + _16 + _8 + _20 + _8 + _25 + 2, _w - _5, _10);
            //备注
            PrintRemark(g, 5, _12 + _16 + _8 + _20 + _8 + _25 + _5 + 5, _w - _5, _25);
            //位置
            PrintWz(g, 5, _12 + _16 + _8 + _20 + _8 + _25 + _5 + _13 + 5, _w - _5, _8);
            //边框
            PrintRectangle(g);
        }

        private void PrintRectangle(Graphics g)
        {
            Pen pen = new Pen(Brushes.Black, 1);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            //横
            g.DrawLine(pen, 0, _12, 280, _12);
            g.DrawLine(pen, 0, _12 + _16, 280, _12 + _16);
            g.DrawLine(pen, 0, _12 + _16 + _8, 280, _12 + _16 + _8);
            g.DrawLine(pen, 0, _12 + _16 + _8 + _20, 280, _12 + _16 + _8 + _20);
            g.DrawLine(pen, 0, _12 + _16 + _8 + _20 + _8, 280, _12 + _16 + _8 + _20 + _8);
            g.DrawLine(pen, 0, _12 + _16 + _8 + _20 + _8 + _25, 280, _12 + _16 + _8 + _20 + _8 + _25);
            g.DrawLine(pen, 0, _12 + _16 + _8 + _20 + _8 + _25 + _5, 280, _12 + _16 + _8 + _20 + _8 + _25 + _5);
        }
        private void PrintPrintTime(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 6, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(now.ToString("yyyy/MM/dd HH:mm:ss"), font, Brushes.Black, rec);
        }
        private void PrintShipName(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 15, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.shipName, font, Brushes.Black, rec);
        }
        private void PrintSortingCode(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 18, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.markDestination, font, Brushes.Black, rec);
        }
        private void PrintCity(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 12, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveCity, font, Brushes.Black, rec);
        }
        private void PrintCurNum(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 12, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            string strNo = printCur.ToString() + "/" + kddh.itemCount;
            g.DrawString(strNo, font, Brushes.Black, rec);
        }
        private void PrintReceive(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.so;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintReceiveMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveMan + "   " + kddh.receiveCompany, font, Brushes.Black, rec);
        }
        private void PrintReceivePhone(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveManPhone, font, Brushes.Black, rec);
        }
        private void PrintReceiveAddress(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.receiveManAddress, font, Brushes.Black, rec);
        }
        private void PrintSend(Graphics g, int x, int y, int w, int h)
        {
            Bitmap img = Resource.ji;
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintSendMan(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(kddh.sendMan + "   " + kddh.sendManPhone, font, Brushes.Black, rec);
        }
        private void PrintRealName(Graphics g, int x, int y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("实名认证", font, Brushes.Black, rec);
        }
        private void PrintHorMailNo(Graphics g, int x, int y, int w, int h)
        {
            EncodingOptions encodeOption = new EncodingOptions();
            encodeOption.Width = 100;
            encodeOption.Height = 30;
            BarcodeWriter wr = new BarcodeWriter();
            wr.Options = encodeOption;
            wr.Format = BarcodeFormat.CODE_128;
            encodeOption.PureBarcode = true;
            System.Threading.Thread.Sleep(100);

            string mailNo;
            if (int.Parse(kddh.itemCount) > 1 && printCur > 1 && kddh.mailno_zd != null && kddh.mailno_zd.Split(',').Length > (printCur - 2))
            {
                mailNo = kddh.mailno_zd.Split(',')[printCur - 2];
            }
            else
            {
                mailNo = kddh.mailNo;
            }
            Bitmap img = wr.Write(mailNo);
            g.DrawImage(img, x, y, w, h);
        }
        private void PrintMailNo(Graphics g, int x, int y, int w, int h)
        {
            Font fontBig = new Font("微软雅黑", 12, FontStyle.Bold);
            Font fontSmall = new Font("微软雅黑", 10, FontStyle.Bold);

            if (int.Parse(kddh.itemCount) > 1)
            {
                if (printCur > 1 && kddh.mailno_zd != null && kddh.mailno_zd.Split(',').Length > (printCur - 2))
                {
                    string zd = kddh.mailno_zd.Split(',')[printCur - 2];
                    string mailNo_Zd = zd.Length > 12 ? zd.Substring(0, 3) + "  " + zd.Substring(3, 4) + "  " + zd.Substring(7, 4) + "  " + zd.Substring(11) : zd;
                    RectangleF recMailNo_Zd = new RectangleF(x, y, w, h);
                    g.DrawString("子 " + mailNo_Zd, fontBig, Brushes.Black, recMailNo_Zd);

                    string mailNo = kddh.mailNo.Length > 12 ? kddh.mailNo.Substring(0, 3) + " " + kddh.mailNo.Substring(3, 4) + " " + kddh.mailNo.Substring(7, 4) + " " + kddh.mailNo.Substring(11) : kddh.mailNo;
                    RectangleF recMailNo = new RectangleF(x + _5, y + _5, w, h);
                    g.DrawString("母 " + mailNo, fontSmall, Brushes.Black, recMailNo);

                }
                else
                {
                    string mailNo = kddh.mailNo.Length > 12 ? kddh.mailNo.Substring(0, 3) + "  " + kddh.mailNo.Substring(3, 4) + "  " + kddh.mailNo.Substring(7, 4) + "  " + kddh.mailNo.Substring(11) : kddh.mailNo;
                    RectangleF recMailNo = new RectangleF(x, y, w, h);
                    g.DrawString("母 " + mailNo, fontBig, Brushes.Black, recMailNo);
                }
            }
            else
            {
                string mailNo = kddh.mailNo.Length > 12 ? kddh.mailNo.Substring(0, 3) + "  " + kddh.mailNo.Substring(3, 4) + "  " + kddh.mailNo.Substring(7, 4) + "  " + kddh.mailNo.Substring(11) : kddh.mailNo;
                RectangleF recMailNo = new RectangleF(x + _10, y, w, h);
                g.DrawString(mailNo, fontBig, Brushes.Black, recMailNo);
            }
        }
        private void PrintLook(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 10, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("已检视", font, Brushes.Black, rec);
        }
        private void PrintInsuranceValue(Graphics g, float x, float y, float w, float h)
        {
            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);

            if (decimal.TryParse(kddh.insuranceValue, out decimal val) && val > 0)
            {
                g.DrawString("保价金额:" + val.ToString(), font, Brushes.Black, rec);
            }
        }
        private void PrintRemark(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.remark;

            Font font = new Font("微软雅黑", 8, FontStyle.Regular);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString("序号:" + printCur + ";" + str, font, Brushes.Black, rec);
        }
        private void PrintWz(Graphics g, float x, float y, float w, float h)
        {
            string str = kddh.wzh;

            Font font = new Font("微软雅黑", 16, FontStyle.Bold);
            RectangleF rec = new RectangleF(x, y, w, h);
            g.DrawString(str, font, Brushes.Black, rec);
        }
    }
}
